<?php
header('Content-type:text/html;charset=utf8;');
if(strtok(PHP_VERSION, '.') < 7){
    exit('需要php7+版本');
}

// ----------------------------
// 日志文件目录展示
// ----------------------------
// 日志根目录
$rootPath = './log/';
// 列表
$list = [];

function getTree($path, $prefix = '', $dir = '')
{
    // 把路径统一成 / 分隔，去掉末尾的 /
    $path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');

    // 打开目录
    if (!$dh = opendir($path)) {
        return;
    }

    // 先排个序，让目录和文件按名字排
    $items = array();
    while (($item = readdir($dh)) !== false) {
        $arr = ['.', '..', '.gitignore'];
        if (in_array($item, $arr)) {
            continue;
        }
        $items[] = $item;
    }
    closedir($dh);
    sort($items);

    foreach ($items as $item) {
        $full = $path . '/' . $item;
        if (is_dir($full)) {
            $GLOBALS['list'][$item] = [
                'type' => 'dir',
                'item' => $prefix . ($item),
                'full' => ($full),
            ];
            // 递归子目录，缩进多 4 个空格
            getTree($full, $prefix . '&nbsp;&nbsp;&nbsp;&nbsp;', $item);

        } else {
            $GLOBALS['list'][$dir]['child'][] = [
                'type' => 'file',
                'item' => $prefix . ($item),
                'full' => ($full),
            ];
        }
    }
}


// 从根目录开始打印
getTree($rootPath);
// echo '<pre>';
// var_dump($list);
// exit;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>nginx访问日志目录</title>
<style type="text/css">
    a{
        color: #0e72cc;
        text-decoration: none;
    }
    p{
        margin: 4px 0;
    }
    .hide{
        display: none;
    }
</style>

</head>
<body>
<div class="main">
    <p>目录（目录不可修改；文件名尽量不用中文和特殊符号）</p>
    <hr>
    <?
        foreach($list as $k => $v){
            // 目录
            echo "<p>{$v['item']}</p>";
            echo "<div class='list'>";
            foreach ($v['child'] as $key => $val) {
                // 文件
                $size = round(filesize($val['full']) / 1024 / 1024, 3);
                $href = '';
                switch (1) {
                    case strpos($val['full'], '/dkw/') !== false:
                        // 大咖玩
                        $href = "./nginx_log.php?file_name={$val['full']}";
                        break;

                    case strpos($val['full'], '/dkw_sdk/') !== false:
                        // 大咖玩sdk
                        $href = "./nginx_log.php?file_name={$val['full']}&type=dkw_sdk";
                        break;

                    case strpos($val['full'], '/yw/') !== false:
                        // 大咖玩sdk
                        $href = "./nginx_log.php?file_name={$val['full']}";
                        break;
                    
                    case strpos($val['full'], '/mysql/') !== false:
                        // mysql慢查询
                        $href = "./mysql_log.php?file_name={$val['full']}";
                        break;

                    default:
                        # code...
                        break;
                }
                echo "<p class='item'><a href='{$href}' target='_blank'>{$val['item']}</a> ({$size}MB)</p>";

            }
            echo "<p>-----------------------------------</p>";
            echo '</div>';
        }
    ?>
    <hr>
</div>

<script src="./static/jquery-3.6.1.min.js" type="text/javascript"></script>
<script src="./static/Chart.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $('.list').each(function(i, e){
        let item = $(e).find('.item');
        let len = item.length;
        let showLen = 7;
        if(len > showLen){
            let hideNum = len - showLen;
            $(e).find('.item:lt(' + hideNum + ')').addClass('hide');
            $(e).prepend('<p class="js-show-all">&nbsp;&nbsp;&nbsp;&nbsp;......展开全部</p>');
        }
    });

    $('.js-show-all').on('click', function(){
        $(this).parent().find('.item.hide').removeClass('hide');
        $(this).remove();
    });
    
</script>
</body>
</html>
